package Net;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.example.zelonghu.testgps.AppContent;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import File.FileUtils;
import static android.content.ContentValues.TAG;

/**
 * @author zelonghu QQ:1032006226
 * @name testGPS2
 * @class name：Net
 * @class describe
 * @time 2019/4/16 4:19 PM
 * @change
 * @chang time
 * @class describe
 */
public class Netutils {
    InputStream inputStream;
    public  InputStream downloadfiles(String path){
        try {
            //将地址封装成URL对象
            URL url = new URL(path);

            //获取连接对象,此时未建立连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            System.out.println("download---》"+conn.getResponseMessage());


            //获得文件输入流

            inputStream = conn.getInputStream();
            if (inputStream == null){
                System.out.println("inputstream------->no stream");
                return null;
            }else {
                System.out.println("inputstream------->" + inputStream);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return inputStream;
    }
    /**
     * 从服务器取图片
     *http://bbs.3gstdy.com
     * @param url
     * @return bitmap
     * @TODO 通过url来获取bigmap的图片，用于显示News的图片
     */
    public static Bitmap getImageHttpBitmap(String url) {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            Log.d(TAG, url);
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setConnectTimeout(0);
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    /**
     * Created by zelonghu on 2018/4/17.
     * @TODO 用于通过线程控制获取BitMapImage
     * @LASTEDIT 2018/4/17
     */
    public class HttpimgThread extends Thread{
        private Handler handler;
        private ImageView iv;
        private String strurl;

        private static final String TAG = "HttpImgThread";

        public HttpimgThread (Handler handler,ImageView iv,String strurl){
            this.handler = handler;
            this.iv = iv;
            this.strurl = strurl;
        }
        @Override
        public void run() {
            URL url = null;
            HttpURLConnection con = null;
            try {
                //创建链接
                url = new URL(strurl);
                con = (HttpURLConnection) url.openConnection();
                //设置链接属性
                con.setRequestMethod("GET");
                con.setReadTimeout(5000);
                con.setDoInput(true);
                //InputStream in = con.getInputStream();
                //新建文件并且获得输出流
                FileUtils fileUtils =  new FileUtils();
                //如果没有主目录就要创建主目录
                if(!fileUtils.isexist(AppContent.APP_DIR)){
                    System.out.println("BuildPath--->" + AppContent.APP_DIR);
                    fileUtils.createdir(AppContent.APP_DIR);
                }
                //如果没有分目录就创建分目录
                if(!fileUtils.isexist(AppContent.APP_DIR + File.separator +  AppContent.ICON_DIR)){
                    System.out.println("BuildPath--->" + AppContent.APP_DIR + File.separator +AppContent.ICON_DIR);
                    fileUtils.createdir(AppContent.APP_DIR + File.separator +AppContent.ICON_DIR);
                }
                File file = new File(Environment.getExternalStorageDirectory()+ File.separator +
                        AppContent.APP_DIR + File.separator + AppContent.ICON_DIR
                        +File.separator +  strurl.substring(AppContent.NEWS_ICON_URL.length()-1,strurl.length()));
                //如果文件不存在，下载
                if(!file.exists()) {
                    FileOutputStream fos = new FileOutputStream(file);
                    //获得输入流
                    InputStream in = con.getInputStream();
                    Log.e(TAG, "run: path---" + file.getAbsolutePath());
                    //规定2k为基本传输单位
                    byte ch[] = new byte[2 * 1024];
                    int len;
                    if (fos != null) {
                        while ((len = in.read(ch)) != -1) {
                            fos.write(ch, 0, len);
                        }
                        in.close();
                        fos.close();
                    }
                }
                //获得bitmap文件
                final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (bitmap != null){
                            System.out.println("add" +strurl );
                            iv.setImageBitmap(bitmap);
                        }
                    }
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        }
}
